package com.example.booktest1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class SoundPoolTest extends Activity implements OnTouchListener {
	StringBuilder builder = new StringBuilder();
	TextView textView;
	SoundPool soundPool;
	int explosionId = -1;
	//	boolean loaded = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textView = new TextView(this);
		textView.setOnTouchListener(this);
		setContentView(textView);

		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		soundPool = new SoundPool(20,AudioManager.STREAM_MUSIC,0);

		try {
			AssetManager assetManager = getAssets();
			AssetFileDescriptor descriptor = assetManager.openFd("sounds/explosion.ogg");

			//			soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
			//	            @Override
			//	            public void onLoadComplete(SoundPool soundPool, int sampleId,
			//	                    int status) {
			//	                loaded = true;
			//	            }
			//	        });

			explosionId = soundPool.load(descriptor, 1);
		} catch (IOException e) {
			textView.setText("Couldn't load sound effect from asset, " + e.getMessage());
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if (explosionId != -1) {
				soundPool.play(explosionId, 1, 1, 0, 0, 1);
			}
		}
		return true;
	}





}
